package controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Game;

public class UpdateSeatServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public UpdateSeatServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		response.setContentType("text/xml");
		response.setCharacterEncoding("UTF-8");
		response.setHeader( "Cache-Control", "no-cache" );
		
		String result = "<response>";

		int numberOfPlayers = 0;

		if (null != getServletContext().getAttribute("game")) {
			
			Game game = (Game) getServletContext().getAttribute("game");
			
			for(int i=0 ; i<9; i++){
				
				result += "<player>";
				if(game.getSeats().get(i).getPlayer() == null){
					
					result += "<name>null</name><status>null</status><avatar>null</avatar><assignment>null</assignment><money>null</money><bet>null</bet><isTurn>null</isTurn>";

		    	} else {
		    		result += "<name>" + game.getSeats().get(i).getPlayer().getName() + "</name>";
		    		result += "<status>" + game.getSeats().get(i).getPlayer().getStatus() + "</status>";
		    		result += "<avatar>" + game.getSeats().get(i).getPlayer().getAvatar() + "</avatar>";
		    		result += "<assignment>" + game.getSeats().get(i).getPlayer().getAssignment() + "</assignment>";
		    		result += "<money>"+game.getSeats().get(i).getPlayer().getMoney()+"</money>";
		    		result += "<bet>"+game.getSeats().get(i).getPlayer().getCurrentBet()+"</bet>";
		    		result += "<isTurn>"+game.getSeats().get(i).getPlayer().isTurn()+"</isTurn>";
		    		
		    		numberOfPlayers++;
		    	}
				result += "</player>";
				
			}
			
			boolean inGame = false;
			boolean inRound = false;
			
			for(int i=0; i<9; i++){
				if(game.getSeats().get(i).getPlayer() != null){
					if(request.getSession().getId() == game.getSeats().get(i).getPlayer().getSessionId()){
						if(game.getSeats().get(i).getPlayer().getCards()[0] != null){
							inRound = true;
							result += "<handCard1>" + game.getSeats().get(i).getPlayer().getCards()[0] + "</handCard1>";
							result += "<handCard2>" + game.getSeats().get(i).getPlayer().getCards()[1] + "</handCard2>";
							result += "<rank>" + game.getSeats().get(i).getPlayer().print() + "</rank>";
							
							if(request.getSession().getId().equals(game.getHolder())) {
								result += "<holder>true</holder>";
								if(game.getRaiseCounter() >= 4)
									result += "<raise>true</raise>";
								else
									result += "<raise>false</raise>";
							}
							else {
								result += "<holder>false</holder>";
								result += "<raise>true</raise>";
							}
						}
						inGame = true;
					}
				}
			}
			
			result += "<numberOfTableCards>"+game.getTableCards().size()+"</numberOfTableCards>";
			
			if(game.getTableCards().size()!=0){
				for(int i=0; i<game.getTableCards().size(); i++)
					result += "<tableCard>"+game.getTableCards().get(i).toString()+"</tableCard>";
			}

			result += "<pot>" + game.getPot() + "</pot>";
			
			if(inGame){
				result += "<inGame>true</inGame>";
			}else{
				result += "<inGame>false</inGame>";
			}
			if(inRound){
				result += "<inRound>true</inRound>";
			}else{
				result += "<inRound>false</inRound>";
			}
			
			if(game.isRoundStarted()){
				result += "<isRoundStarted>true</isRoundStarted>";
			}else{
				result += "<isRoundStarted>false</isRoundStarted>";
			}
			
			if(game.isGameOver()) {
				result += "<isOver>true</isOver>";
				result += "<cards>"+game.showdown()+"</cards>";
				result += "<winner>"+game.getWinner().get(0).getName()+"</winner>";
			} else {
				result += "<isOver>false</isOver>";
				result += "<cards>null</cards>";
				result += "<winner>null</winner>";
			}
			
			result += "<numberOfPlayers>" + numberOfPlayers + "</numberOfPlayers>";
		} else {
			
			for(int i = 0; i<9; i++){
				result += "<player><name>null</name><status>Waiting</status></player>";
			}
			result += "<numberOfTableCards>0</numberOfTableCards><isRoundStarted>false</isRoundStarted><inGame>false</inGame><numberOfPlayers>0</numberOfPlayers><pot>null</pot><isOver>false</isOver><cards>null</cards><winner>null</winner>";
			
		}
		
		result += "</response>";
		out.print(result);
		out.close();
 	}
}